package cn.loioi.util.page.query.where;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * 过滤规则
 * @file_name :QueryFilter.java
 * @author :loioi.cn
 * @创建时间 :2016年10月31日 上午11:31:51
 */
public class QueryFilter implements Serializable {

	private static final long serialVersionUID = -2978780165641745068L;
	private QueryGroup queryGroup = QueryGroup.AND;
	private String field;
	private QueryRule queryRule = QueryRule.EQ;
	private Object data;

	public QueryFilter() {
	}

	public QueryFilter(String field, Object data) {
		this.field = field;
		this.data = data;
	}

	public QueryFilter(String field, QueryRule queryRule, Object data) {
		this(field, data);
		this.queryRule = queryRule;
	}

	public QueryFilter(QueryGroup queryGroup, String field, QueryRule queryRule, Object data) {
		this(field, queryRule, data);
		this.queryGroup = queryGroup;
	}

	/**
	 * 获取查询连接
	 * @return
	 */
	public QueryGroup getQueryGroup() {
		return queryGroup;
	}

	/**
	 * 设置查询连接
	 * @param queryGroup
	 */
	public void setQueryGroup(QueryGroup queryGroup) {
		this.queryGroup = queryGroup;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public QueryRule getQueryRule() {
		return queryRule;
	}

	public void setQueryRule(QueryRule queryRule) {
		this.queryRule = queryRule;
	}

	public Object getData() {
		return data;
	}

	public String getDataStr() {
		return (data == null ? "" : data.toString());
	}

	public Collection<?> getDataArray() {
		if (data == null) {
			return new ArrayList<>();
		} else if (data instanceof Iterable) {
			return (Collection<?>) data;
		} else if (data.getClass().isArray()) {
			return Arrays.asList((Object[]) data);
		} else {
			return Arrays.asList(data.toString().split(","));
		}
	}

	public void setData(Object data) {
		this.data = data;
	}

}
